package com.ccdt.tv.smartrecommend.view.web;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;

/**
 * Created by sheng on 2018/9/12.
 */

public class SRWebViewClient extends WebViewClient {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        //设定加载开始的操作
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    @Override
    public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
        Log.w("syt", "shouldOverrideKeyEvent: action:" + event.getAction()
                + "--code" + event.getKeyCode());
//        switch (event.getKeyCode()) {
//            case KeyEvent.ACTION_DOWN:
//            case KeyEvent.ACTION_UP:
//            case KeyEvent.KEYCODE_DPAD_UP:
//            case KeyEvent.KEYCODE_DPAD_DOWN:
//            case KeyEvent.KEYCODE_DPAD_RIGHT:
//            case KeyEvent.KEYCODE_DPAD_LEFT:
//                break;
//            case KeyEvent.KEYCODE_ENTER:
//            case KeyEvent.KEYCODE_DPAD_CENTER:
//                hideInputManager(Utils.getApp(),view);
//                break;
//        }
        if (event.getAction() == KeyEvent.ACTION_UP){
            switch (event.getKeyCode()) {
                case KeyEvent.KEYCODE_ENTER:
                case KeyEvent.KEYCODE_DPAD_CENTER:
                    hideInputManager(Utils.getApp(),view);
                    break;
            }
        }
        return super.shouldOverrideKeyEvent(view, event);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        Log.w("syt", "onPageFinished: 加载完毕：" + url);
        super.onPageFinished(view, url);
        view.requestFocus();
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        Log.w("syt", "onReceivedError: errorCode:" + errorCode + "--description:" + description);
        switch (errorCode) {
            case 404:
//                view.loadUrl("file:///android_assets/error_handle.html");
                ToastUtils.showShort("网路异常，请稍后重试");
            case 500:
//                view.loadUrl("file:///android_assets/error_handle.html");
                ToastUtils.showShort("服务器异常，请稍后重试");
                break;
        }
    }

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();    //表示等待证书响应
        // handler.cancel();      //表示挂起连接，为默认方式
        // handler.handleMessage(null);    //可做其他处理
    }

    /**
     * 隐藏输入软键盘
     * @param context
     * @param view
     */
    public static void hideInputManager(Context context, View view){
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()){
//            ToastUtils.showShort("请用遥控器数字键盘输入内容");
        }
        if (view !=null && imm != null){
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏
        }
    }
}
